﻿using EntityCreater.Entity;
using EntityCreater.Service;
using NetDimension.WinForm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EntityCreater
{
    public partial class FrmConnStrConfig : ModernUIForm
    {
        public FrmConnStrConfig()
        {
            InitializeComponent();
            this.lblTitle.Font = new System.Drawing.Font("微软雅黑", 19F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        }
        #region 样式
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        #endregion


        private void FrmConnStrConfig_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        private void FrmConnStrConfig_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            var server = txtServer.Text.Trim();
            var database = txtDataBase.Text.Trim();
            var uid = txtUID.Text.Trim();
            var pwd = txtPWD.Text.Trim();
            var connStr = $"server={server};database={database};uid={uid};pwd={pwd}";
            var tuple = DBService.ConnectIsSuccess(connStr);
            if (tuple.Item1)
            {
                ALLConfig.Server = server;
                ALLConfig.DataBase = database;
                ALLConfig.UID = uid;
                ALLConfig.PWD = pwd;
                ALLConfig.ConnStr = connStr;
                MessageBox.Show("链接成功");
            }
            else
            {
                MessageBox.Show(tuple.Item2);
            }
        }
    }

}
